//
//  SettingViewController.swift
//  jiahui
//
//  Created by 3tee on 2024/11/21.
//

import UIKit
import RxCocoa
import RxSwift

class SettingViewController: UITableViewController {
    
    @IBOutlet weak var nameLab: UILabel!
    var loginModel: LoginModel?
    
    @IBOutlet weak var videoSwitch: UISwitch!
    
    @IBOutlet weak var audioSwitch: UISwitch!
    
    @IBOutlet weak var msgSwitch: UISwitch!
    
    @IBOutlet weak var timeSwitch: UISwitch!
    
    @IBOutlet weak var logoutBtn: UIButton!
    
    @IBOutlet weak var icon: UIImageView!
    let bag = DisposeBag()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .systemGray6
        setupUI()
        switchsHandler()
        icon.image = createImageWithText()
        icon.backgroundColor = .green

    }
    
    override func viewDidAppear(_ animated: Bool) {
        let isLogin = UserDefaultsPlugin.bool(forKey: Constant.key_isLogin)
        if isLogin{
            let data = UserDefaultsPlugin.obj(forKey: Constant.key_loginModel) as! Data
            if let loginModel2 = try? JSONDecoder().decode(LoginModel.self, from: data){
                print("已经登陆过 保存的model = \(loginModel2)")
                self.loginModel = loginModel2
                nameLab.text = loginModel?.name
                icon.image = createImageWithText()
            }
        }
       
       
        
        
    }
    
    func setupUI(){
        title = "设置"
        let isLogin = UserDefaultsPlugin.bool(forKey: Constant.key_isLogin)
        logoutBtn.isHidden = !isLogin
        let videoOn = UserDefaultsPlugin.bool(forKey: Constant.key_video)
        videoSwitch.setOn(videoOn, animated: true)
        audioSwitch.setOn(UserDefaultsPlugin.bool(forKey: Constant.key_audio), animated: true)
        msgSwitch.setOn(UserDefaultsPlugin.bool(forKey: Constant.key_msg), animated: true)
        timeSwitch.setOn(UserDefaultsPlugin.bool(forKey: Constant.key_meeting_time), animated: true)

    }
    
    func switchsHandler(){
        videoSwitch.rx.isOn.subscribe { isOn in
            print("视频开关当前是\(isOn)")
            UserDefaultsPlugin.setValue(Bool(isOn), key: Constant.key_video)
        }.disposed(by: bag)
        
        audioSwitch.rx.isOn.subscribe { isOn in
            print("音频开关当前是\(isOn)")
            UserDefaultsPlugin.setValue(Bool(isOn), key: Constant.key_audio)

        }.disposed(by: bag)
        
        msgSwitch.rx.isOn.subscribe { isOn in
            print("消息开关当前是\(isOn)")
            UserDefaultsPlugin.setValue(Bool(isOn), key: Constant.key_msg)

        }.disposed(by: bag)
        
        timeSwitch.rx.isOn.subscribe { isOn in
            print("会议时长开关当前是\(isOn)")
            UserDefaultsPlugin.setValue(Bool(isOn), key: Constant.key_meeting_time)

        }.disposed(by: bag)
        
        logoutBtn.rx.tap.subscribe { _ in
            UserDefaultsPlugin.setValue(false, key: Constant.key_isLogin)
            UserDefaultsPlugin.setValue(nil, key: Constant.key_loginModel)
            if let window = UIApplication.shared.delegate?.window{
                window?.rootViewController = UINavigationController(rootViewController: JoinViewController())
            }
        }.disposed(by: bag)
    }

    @IBAction func logoutClicked(_ sender: UIButton) {
  
    }
    
    
}


extension SettingViewController{

    func createImageWithText() -> UIImage? {
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: 50, height: 50))
        let image = renderer.image { (context) in
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .center
            let attributes: [NSAttributedString.Key: Any] = [
               .font: UIFont.systemFont(ofSize: 24),
               .foregroundColor: UIColor.black,
               .paragraphStyle: paragraphStyle
            ]
            let text = "zqj"
            let textRect = CGRect(x: 0, y: 0, width: 50, height: 50)
            text.draw(in: textRect, withAttributes: attributes)
        }
    
        
        return image
    }
}
